﻿
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AgvDemo
{
    public class BasicFunctionClass
    {
        public static object locker = new object();//防止下位机数据处理异常
        public static object lockCallBox = new object();//防止下位机数据处理异常
        public static object lockStation = new object();
        public static object lockAgv = new object();
        public static object lockError = new object();
        BLL.Fault_Record faultRecordBll = new BLL.Fault_Record();
        /// <summary>
        /// AGV数据处理
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="receiveBytes"></param>
        /// <param name="agvF"></param>
        /// <returns></returns>


        /// <summary>
        /// AGV状态显示
        /// </summary>
        /// <param name="agvInfo"></param>
        /// <param name="ip"></param>


        /// <summary>
        /// 异常监控
        /// </summary>
        /// <param name="agvInfo"></param>
        /// <summary>
        /// 异常监控
        /// </summary>
        /// <param name="agvInfo"></param>
        public void InsertFault(int num, int code, int station)
        {
            try
            {
                Model.Fault_Record fr = new Model.Fault_Record();
                fr.agv_num = num;
                fr.fault_code = code;
                fr.occur_station = station;
                fr.occur_time = DateTime.Now;
                faultRecordBll.Add(fr);
            }
            catch
            {
            }
        }

        public void ErrorMonitor(ChargeParaClass okAgv)
        {
            try
            {
                if (okAgv.AgvNum > 0)
                {
                    if (okAgv.AgvError == 1 || okAgv.AgvError == 2 || okAgv.AgvError == 4 || okAgv.AgvError == 8 || okAgv.AgvError == 16 || okAgv.AgvError == 32 || okAgv.AgvError == 64)
                    {
                        if (okAgv.AgvError != AgvFunctionClass.AgvErrorDic[okAgv.AgvNum])
                        {
                            InsertFault(okAgv.AgvNum, okAgv.AgvError, okAgv.CurrentStation);

                            AgvFunctionClass.AgvErrorDic[okAgv.AgvNum] = okAgv.AgvError;

                            #region agv报警上传至iot
                            //AgvAlarmClass aac = new AgvAlarmClass();
                            //aac.serno = DateTime.Now.ToString("yyyyMMddHHmmss") + AgvFunctionClass.GetRandomNumberOther(100000, 999999);
                            //aac.eqp_num = "dev" + okAgv.AgvNum;
                            //aac.alarm_status = "发生";
                            
                            //aac.alarm_code = okAgv.AgvError.ToString();

                            //switch (okAgv.AgvError)
                            //{
                            //    case 1: aac.alarm_msg = "驱动升降错误"; aac.alarm_type = "中"; break;
                            //    case 2: aac.alarm_msg = "牵引升降错误"; aac.alarm_type = "中"; break;
                            //    case 4: aac.alarm_msg = "无路径错误"; aac.alarm_type = "低"; break;
                            //    case 8: aac.alarm_msg = "通信板超时"; aac.alarm_type = "低"; break;
                            //    case 16: aac.alarm_msg = "急停错误"; aac.alarm_type = "低"; break;
                            //    case 32: aac.alarm_msg = "防撞错误"; aac.alarm_type = "低"; break;
                            //    case 64: aac.alarm_msg = "驱动器故障"; aac.alarm_type = "中"; break;
                            //}
                            //aac.alarm_time = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
                            //string postData = JsonConvert.SerializeObject(aac);
                            //PostToServer.Post("http://10.2.5.170:8113/web/httpServlet/api/agvReportEquipmentAlarm", postData);
                            //AgvFunctionClass.AgvAlarmList.Add(aac);
                            #endregion
                        }

                    }
                    else if (okAgv.AgvError == 0 && AgvFunctionClass.AgvErrorDic[okAgv.AgvNum] != 0)
                    {
                        AgvFunctionClass.AgvErrorDic[okAgv.AgvNum] = 0;

                        #region agv报警上传至iot
                        //AgvAlarmClass aac = new AgvAlarmClass();
                        //aac.serno = DateTime.Now.ToString("yyyyMMddHHmmss") + AgvFunctionClass.GetRandomNumberOther(100000, 999999);
                        //aac.eqp_num = "dev" + okAgv.AgvNum;
                        //aac.alarm_status = "解除";
                        //aac.alarm_type = "";
                        //aac.alarm_code = "0";
                        //aac.alarm_msg = "正常";                       
                        //aac.alarm_time = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
                        //string postData = JsonConvert.SerializeObject(aac);
                        //PostToServer.Post("http://10.2.5.170:8113/web/httpServlet/api/agvReportEquipmentAlarm", postData);
                        //AgvFunctionClass.AgvAlarmList.Add(aac);
                        #endregion
                    }
                }
            }
            catch
            { }
           

        }

        /// <summary>
        /// 站点记录
        /// </summary>
        /// <param name="agvInfo"></param>

        public void StationRecord(ChargeParaClass plcAgv)
        {

            try
            {
                List<int> tempNum = new List<int>(AgvFunctionClass.AgvOldStationDic.Keys);
                for (int i = 0; i < AgvFunctionClass.AgvOldStationDic.Count; i++)
                {
                    if (tempNum[i] == plcAgv.AgvNum)
                    {
                        if (plcAgv.CurrentStation != 0 && AgvFunctionClass.AgvOldStationDic[tempNum[i]] != plcAgv.CurrentStation)
                        {
                            AgvFunctionClass.AgvOldStationDic[tempNum[i]] = plcAgv.CurrentStation;
                            AgvStationClass asc = new AgvStationClass();
                            asc.AgvNum = plcAgv.AgvNum;
                            asc.Station = plcAgv.CurrentStation;
                            asc.CurrentTime = DateTime.Now.ToString();
                            AgvFunctionClass.stationRecordList.Add(asc);


                         

                            break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
            }
        }
             

        
    }
}
